clear all;
clc;
%%%%%%%%% Programming Language: Matlab 2019b       %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% (C) Andrew B. Abel and Stavros Panageas 2021 %%%%%%%%%%%%%%%%%%
%%%%%%%%% This code produces Figure 1 in the paper %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% "An Analytic Framework for Interpreting %%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% Investment Regressions in the Presence of Financial Constraints
%%%%%%%%% NOTE: The positioning of text labels in the figures may differ
%%%%%%%%% from the figures of the paper


%%%%%%%%% Parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rho=0.07;
mu_H=0.3;
mu_L=0.1;
phi_L=-0.1;
phi_H=0.2;
alpha=0.0;

%%%%%%%%% Computation of x^{\ast} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
eta_1=(rho+mu_H)/phi_L;
eta_2=(rho+mu_L)/phi_H;
A=[eta_2 -mu_L/phi_H;-mu_H/phi_L eta_1];
om=eig(A);
om_1=om(2);
om_2=om(1);
x_m=max(1/(om_2-om_1)*log(om_1^2/om_2^2*(eta_2-om_2)/(eta_2-om_1)),0); % This is the analytical solution for x^{\ast} assuming no salvage value


%%%%%%%%%%% Computation of F(x,H) and F(x,L) %%%%%%%%%%%%%%%%%%%%%%%%%
m_H=mu_L/phi_H;
m_L=mu_H/phi_L;
y_0=1/mu_L*exp(-(m_L+m_H)*x_m) - (1/phi_H -1/phi_L)/(m_H+m_L)*(exp(-(m_L+m_H)*x_m)-1)

F_L=@(x) (1/y_0)*(1/phi_L)/(m_L+m_H)*(exp(-(m_L+m_H)*x)-1); % Equation 25 in the text
F_H=@(x) (1/y_0)*(-1/phi_H)/(m_L+m_H)*(exp(-(m_L+m_H)*x)-1);

%%%%%%%%%%% Figures: Please note that the labels for the curves may not
%%%%%%%%%%% appear at the right positions %%%%%%%%%%%%%%%%%

figure
FS=14;
plot([0:0.001:x_m],F_H(0:0.001:x_m),'Color','blue')
hold on
plot([0:0.001:x_m],F_L(0:0.001:x_m),'Color','red')
hold on
plot([0:0.001:x_m,x_m+0.0001],[F_L(0:0.001:x_m)+F_H(0:0.001:x_m),1],'Color','black')
ylabel('$F(x,H),F(x,L)$','Interpreter','latex','FontSize',FS)
xlabel('$x$','Interpreter','latex','FontSize',FS)
grid('off')
str={'$F(x,L)$'};
textbo=annotation('textarrow',[0.6,0.6],[0.6 0.5],'String',str,'Interpreter','latex')
textbo.Interpreter='latex';
textbo.FontSize=FS;
str={'$F(x,H)$'};
textbo=annotation('textarrow',[0.7,0.7],[0.3 0.2],'String',str,'Interpreter','latex')
textbo.Interpreter='latex';
textbo.FontSize=FS;
str={'$F(x,H)+F(x,L)$'};
textbo=annotation('textarrow',[0.4,0.4],[0.8 0.7],'String',str,'Interpreter','latex')
textbo.Interpreter='latex';
textbo.FontSize=FS;
str={'Mass point at $x^{\ast}$'};
textbo=annotation('textarrow',[0.4,0.4],[0.8 0.7],'String',str,'Interpreter','latex')
textbo.Interpreter='latex';
textbo.FontSize=FS;
box('off')
